from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class UserPage():

    def __init__(self, driver):
        self.driver = driver
        # self.url = "http://localhost:8080/user"
        self.url = "http://wj53793py34.vicp.fun/user"
        # 新增页面xpath
        self.add_user_btn_xpath = "//span[text()=' 新增json ']"
        self.username_xpath = "//label[text()='用户名']/following::input[1]"
        self.password_xpath = "//input[@type='password']"
        self.gender_xpath = "//span[text()='女']"
        self.education_xpath1 = "//input[@placeholder='请选择']"
        self.education_xpath2 = "//span[text()='博士']"
        self.date_xpath = "//input[@placeholder='选择日期']"
        self.hobby_xpath = "//span[text()='游泳']"
        self.brief_xpath = "//textarea"
        self.confirm_btn_xpath = "//span[text()='确定']"
        self.add_alert_xpath = "//p[text() = '新增用户成功']"

        # 编辑xpath，仅修改用户名
        self.edit_btn_xpath = "//span[text()='编辑'][1]"
        self.edit_username_xpath = "(//div[@aria-label='编辑']//label[text()='用户名']/following::input)[1]"
        self.edit_confirm_btn_xpath = "//div[@aria-label='编辑']//span[text()='确定']"
        self.edit_alert_xpath = "//p[text() = '编辑用户成功']"

        # 查询xpath
        self.search_input_xpath = "//input[@placeholder='请输入用户名']"
        self.search_btn_xpath = "//span[text()='搜索']"
        # 列表第一条数据，名字字段的xpath
        self.first_line_username_xpath = "//table[@class='el-table__body']//tr[1]//descendant::div[2]"
        # self.first_line_username_xpath = "//table[@class='el-table__body']/tbody[1]/tr[1]/td[2]"

        # 删除xpath
        self.delete_btn_xpath = "//span[text()='删除'][1]"
        self.delete_confirm_btn_xpath = "//div[@aria-label='提示']//span[text()='确定']"
        self.del_alert_xpath = "//p[text() = '删除成功']"

    # 新增
    @property
    def add_user_btn_ele(self):
        """
        获取新增用户按钮
        :return:
        """
        wait = WebDriverWait(self.driver, 10)
        add_user_btn = wait.until(EC.presence_of_element_located((By.XPATH, self.add_user_btn_xpath)))
        return add_user_btn

    @property
    def add_username_ele(self):
        """
        获取新增用户页面的用户名元素
        :return:
        """
        wait = WebDriverWait(self.driver, 10)
        add_username = wait.until(EC.presence_of_element_located((By.XPATH, self.username_xpath)))
        return add_username

    @property
    def add_password_ele(self):
        """
        获取新增用户页面的密码元素
        :return:
        """
        wait = WebDriverWait(self.driver, 10)
        add_password = wait.until(EC.presence_of_element_located((By.XPATH, self.password_xpath)))
        return add_password

    @property
    def add_gender_ele(self):
        """
        获取新增用户页面的性别元素
        :return:
        """
        wait = WebDriverWait(self.driver, 10)
        add_gender = wait.until(EC.presence_of_element_located((By.XPATH, self.gender_xpath)))
        return add_gender

    @property
    def add_education_ele1(self):
        """
        获取新增用户页面的学历元素1
        EC.presence_of_element_located()：等待元素存在，即元素已经在页面上渲染完成
        :return:
        """
        wait = WebDriverWait(self.driver, 10)
        add_education1 = wait.until(EC.presence_of_element_located((By.XPATH, self.education_xpath1)))
        return add_education1

    @property
    def add_education_ele2(self):
        """
        获取新增用户页面的学历元素2
        EC.visibility_of_element_located()：等待元素存在且可交互
        :return:
        """
        wait = WebDriverWait(self.driver, 10)
        add_education2 = wait.until(EC.visibility_of_element_located((By.XPATH, self.education_xpath2)))
        return add_education2

    @property
    def add_date_ele(self):
        """
        获取新增用户页面的日期元素1
        :return:
        """
        wait = WebDriverWait(self.driver, 10)
        add_date = wait.until(EC.presence_of_element_located((By.XPATH, self.date_xpath)))
        return add_date

    @property
    def add_hobby_ele(self):
        """
        获取新增用户页面的爱好元素
        :return:
        """
        wait = WebDriverWait(self.driver, 10)
        add_hobby = wait.until(EC.presence_of_element_located((By.XPATH, self.hobby_xpath)))
        return add_hobby

    @property
    def add_brief_ele(self):
        """
        获取新增用户页面的简介元素
        :return:
        """
        wait = WebDriverWait(self.driver, 10)
        add_brief = wait.until(EC.presence_of_element_located((By.XPATH, self.brief_xpath)))
        return add_brief

    @property
    def add_confirm_btn_ele(self):
        """
        获取新增用户页面的确认元素
        :return:
        """
        wait = WebDriverWait(self.driver, 10)
        add_confirm_btn = wait.until(EC.presence_of_element_located((By.XPATH, self.confirm_btn_xpath)))
        return add_confirm_btn

    @property
    def add_alert_ele(self):
        """
        获取新增用户后的提示信息
        :return:
        """
        wait = WebDriverWait(self.driver, 10)
        add_alert = wait.until(EC.presence_of_element_located((By.XPATH, self.add_alert_xpath)))
        return add_alert

    # 编辑
    @property
    def edit_btn_ele(self):
        """
        获取用户列表中第一个编辑按钮
        :return:
        """
        wait = WebDriverWait(self.driver, 10)
        edit_btn = wait.until(EC.visibility_of_element_located((By.XPATH, self.edit_btn_xpath)))
        return edit_btn

    @property
    def edit_username_ele(self):
        """
        获取编辑页面的用户名元素
        :return:
        """
        wait = WebDriverWait(self.driver, 10)
        edit_username = wait.until(EC.visibility_of_element_located((By.XPATH, self.edit_username_xpath)))
        return edit_username

    @property
    def edit_confirm_btn_ele(self):
        """
        获取编辑页面的确定按钮
        :return:
        """
        wait = WebDriverWait(self.driver, 10)
        edit_confirm_btn = wait.until(EC.visibility_of_element_located((By.XPATH, self.edit_confirm_btn_xpath)))
        return edit_confirm_btn

    @property
    def edit_alert_ele(self):
        """
        获取编辑用户后的提示信息
        :return:
        """
        wait = WebDriverWait(self.driver, 10)
        edit_alert = wait.until(EC.visibility_of_element_located((By.XPATH, self.edit_alert_xpath)))
        return edit_alert

    # 查询
    @property
    def search_input_ele(self):
        """
        获取查询条件输入框
        :return:
        """
        wait = WebDriverWait(self.driver, 10)
        search_input = wait.until(EC.visibility_of_element_located((By.XPATH, self.search_input_xpath)))
        return search_input

    @property
    def search_btn_ele(self):
        """
        获取查询按钮
        :return:
        """
        wait = WebDriverWait(self.driver, 10)
        search_btn = wait.until(EC.visibility_of_element_located((By.XPATH, self.search_btn_xpath)))
        return search_btn

    @property
    def first_line_username_ele(self):
        """
        获取查询按钮
        :return:
        """
        wait = WebDriverWait(self.driver, 10)
        first_line_username = wait.until(EC.visibility_of_element_located((By.XPATH, self.first_line_username_xpath)))
        return first_line_username

    # 删除
    @property
    def delete_btn_ele(self):
        """
        获取用户列表的第一个删除按钮
        :return:
        """
        wait = WebDriverWait(self.driver, 10)
        delete_btn = wait.until(EC.visibility_of_element_located((By.XPATH, self.delete_btn_xpath)))
        return delete_btn

    @property
    def delete_confirm_btn_ele(self):
        """
        获取删除用户时出现的二次确认按钮
        :return:
        """
        wait = WebDriverWait(self.driver, 10)
        delete_confirm_btn = wait.until(EC.visibility_of_element_located((By.XPATH, self.delete_confirm_btn_xpath)))
        return delete_confirm_btn

    @property
    def del_alert_ele(self):
        """
        获取删除用户后的提示信息
        :return:
        """
        wait = WebDriverWait(self.driver, 10)
        del_alert = wait.until(EC.visibility_of_element_located((By.XPATH, self.del_alert_xpath)))
        return del_alert
        